package com.shu.auth.service.impl;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.shu.auth.dao.SysDeptMapper;
import com.shu.auth.dto.DeptLevelDto;
import com.shu.auth.model.SysDept;
import com.shu.auth.service.ITreeService;
import com.shu.auth.utils.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("treeService")
public class TreeServiceImpl implements ITreeService {
    @Resource
    SysDeptMapper sysDeptMapper;
    @Override
    public List<DeptLevelDto> deptTree() {
        //查询所有部门
        List<SysDept> deptList = sysDeptMapper.queryAllDept();
        //new list
        List<DeptLevelDto> dtoList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(deptList)){
            return dtoList;
        }
        for (SysDept dept:deptList){
            //dept适配成dto
            DeptLevelDto deptLevelDto = DeptLevelDto.adapt(dept);
            dtoList.add(deptLevelDto);
        }
        return deptListToTree(dtoList);
    }

    /**
     * 部门树
     * @param deptLevelList
     * @return
     */
    public List<DeptLevelDto> deptListToTree(List<DeptLevelDto> deptLevelList){
        //放入多个key，可以一次性根据key取出来
        Multimap<String,DeptLevelDto> levelDeptMap= ArrayListMultimap.create();

        List<DeptLevelDto> rootList = Lists.newArrayList();
        for (DeptLevelDto dto:deptLevelList){
            //所有部门放入levelDeptMap
            levelDeptMap.put(dto.getLevel(),dto);
            //如果是root目录放入rootList中
            if (LevelUtil.ROOT.equals(dto.getLevel())){
                rootList.add(dto);
            }
        }
        //按照seq从小到大的排序
        Collections.sort(rootList, deptSeqComparator);
        //递归生成树
        transFromDeptTree(rootList,LevelUtil.ROOT,levelDeptMap);

        return rootList;
    }

    public  void  transFromDeptTree(List<DeptLevelDto> deptLevelList,String level,Multimap<String,DeptLevelDto> levelDeptMap){
        for (int i=0;i<deptLevelList.size();i++){
            //遍历该层的每个元素
            DeptLevelDto dto =deptLevelList.get(i);
            //处理当前层级的数据
            String nextLevel=LevelUtil.calculateLevel(level,dto.getId());
            //处理下一层
            List<DeptLevelDto> tempDeptList=(List<DeptLevelDto>)levelDeptMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempDeptList)){
                //排序
                //按照seq从小到大的排序
                Collections.sort(tempDeptList, deptSeqComparator);
                //设置下一层部门
                dto.setDeptList(tempDeptList);
                //进入下一层处理
                transFromDeptTree(deptLevelList,nextLevel,levelDeptMap);
            }

        }
    }

    public Comparator<DeptLevelDto> deptSeqComparator=new Comparator<DeptLevelDto>() {
        @Override
        public int compare(DeptLevelDto o1, DeptLevelDto o2) {
            return o1.getSeq()-o2.getSeq();
        }
    };
}
